﻿package game.account.sub {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageDisplayState;
	import flash.display.DisplayObject;
	import game.ui.Button;
	import game.ui.DynamicClip;
	import game.objects.DynamicObject;
	import game.objects.GameObject;
	import game.account.preview.Preview;
	import game.utils.Screenshot;
	import game.errors.GameError;
	
	public class Sub extends DynamicClip {
		
		private function get btnFullscreen():Button	{ return getChildByName('btnFull')			as Button;	}
		private function get btnVolumeOn():Button	{ return getChildByName('btnVolumeOn')		as Button;	}
		private function get btnVolumeOff():Button	{ return getChildByName('btnVolumeOff')		as Button;	}
		private function get btnScreenshot():Button	{ return getChildByName('btnScreenshot')	as Button;	}
		
		public function Sub():void {
			var skin:DynamicObject = config.getSkin(DynamicObject.SUB_MENU);
			
			super(skin);
			
			if (!skin) return;
			
			if (btnFullscreen)
				btnFullscreen.addEventListener(MouseEvent.CLICK, toogleFullscreen);
			if (btnVolumeOn)
				music.buttonOn = btnVolumeOn;
			if (btnVolumeOff)
				music.buttonOff = btnVolumeOff;
			if (btnScreenshot)
				btnScreenshot.addEventListener(MouseEvent.CLICK, screenshotHandler);
			
		}
		
		private function toogleFullscreen(event:MouseEvent):void {
			if (!stage) return;
			
			switch(stage.displayState) {
				case StageDisplayState.NORMAL:
					stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;    
				break;
				case StageDisplayState.FULL_SCREEN_INTERACTIVE:
				default:
					stage.displayState = StageDisplayState.NORMAL;    
				break;
			}
		}
		
		private function screenshotHandler(event:MouseEvent):void {
			var p:GameObject = this.parent as GameObject;
			if (!p) return;
			
			var object:DisplayObject;
			for (var i:int = 0; i<p.numChildren; i++) {
				if (p.getChildAt(i) is Preview) {
					object = p.getChildAt(i) as DisplayObject;
					break;
				}
			}
			
			if (object)
				setScreenshot(object);
			else {
				error(GameError.GAME_ERROR, 2);
			}
		}
		
		private function setScreenshot(object:DisplayObject):void {
			Screenshot.save(object);
		}
		
		
		
	}
	
}